package com.lxt.blog.dao;

import com.lxt.blog.pojo.Article;
import com.lxt.blog.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ArticleDaoImpl implements ArticleDao {
    //插入一篇文章
    public boolean insertArticle(Article article) {
        Connection conn = null;
        PreparedStatement stmt = null;
        int status = 0;
        try{
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false);
            String sql = "insert into t_article values(null,?,?,?,?,?)";
            stmt=conn.prepareStatement(sql);
            stmt.setString(1,article.getUserid());
            stmt.setString(2,article.getCaption());
            stmt.setString(3,article.getArticle());
            stmt.setString(4,article.getTime());
            stmt.setString(5,article.getPublice());
            status = stmt.executeUpdate();
            conn.commit();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,stmt,null);
        }
        return status == 1 ? true:false;
    }

    //删除一篇文章
    @Override
    public boolean deleteArticle(int id) {
        Connection conn = null;
        PreparedStatement stmt = null;
        int status = 0;
        try{
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false);
            String sql = "delete from t_article where id = ?";
            stmt=conn.prepareStatement(sql);
            stmt.setInt(1,id);
            status = stmt.executeUpdate();
            conn.commit();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,stmt,null);
        }
        return status == 1 ? true:false;
    }

    //查询所有文章
    @Override
    public List<Article> selectUserArticle(String userId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet resultSet = null;
        String sql;
        List<Article> articles = new ArrayList<>();

        try{
            conn = DBUtil.getConnection();
            if("all".equals(userId)){
                sql = "select id,userid,caption,article,time,publicd from t_article where publicd = 'F' order by time DESC ";
                stmt=conn.prepareStatement(sql);
            }else{
                sql = "select id,userid,caption,article,time,publicd from t_article where userid=? order by time DESC ";
                stmt=conn.prepareStatement(sql);
                stmt.setString(1,userId);
            }
            resultSet = stmt.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String userid = resultSet.getString("userid");
                String caption = resultSet.getString("caption");
                String article = resultSet.getString("article");
                String time = resultSet.getString("time");
                String publice = resultSet.getString("publicd");
                articles.add(new Article(id,userid,caption,article,time,publice));
            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,stmt,null);
        }
        return articles;
    }

    //查询文章总数
    @Override
    public int selectArticleSize(String userId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        String sql;
        if ("NULL" != userId) {
            sql = "select count(*) as articleSize from t_article where userid = ?";
        } else {
            sql = "select count(*) as articleSize from t_article";
        }
        int pageSize = 0;
        try{
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false);
            stmt=conn.prepareStatement(sql);
            if ("NULL" != userId) {
                stmt.setString(1,userId);
            }
            ResultSet resultSet = stmt.executeQuery();
            resultSet.next();
            pageSize = resultSet.getInt("articleSize");
            conn.commit();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,stmt,null);
        }
        return pageSize;
    }
}
